//
//  LBMOAuthViewController.swift
//  微博
//
//  Created by 刘保满 on 16/5/22.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SVProgressHUD


class LBMOAuthViewController: UIViewController {
    
    //声明webView属性
    let webView = UIWebView()
    
    override func loadView() {
        //替换控制器的根视图
        view = webView
        //设置webview的代理
        webView.delegate = self
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置导航条
        setNavBar()
        loadOauthPage()
        
    }
    
    //修复在跳转到之前界面之后,提示圆圈还在的问题
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        SVProgressHUD.dismiss()
        
    }
    
    
    
    //打开授权界面 请求授权
    private func loadOauthPage() {
        
        let urlstring = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        
        let url = NSURL(string: urlstring)!
        
        let request = NSURLRequest(URL: url)
        
        webView.loadRequest(request)
        
        
    }
    
    
    
}


//增加一个分类,设置导航条
extension LBMOAuthViewController {
    
    private func setNavBar() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", target: self, action: #selector(LBMOAuthViewController.closeBtnClick))
        
    }
    //关闭按钮点击事件
    @objc private func closeBtnClick() {
        
        dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    
    
}


extension LBMOAuthViewController:UIWebViewDelegate {
    
    //代理方法
    func webViewDidStartLoad(webView: UIWebView) {
        //等待指示器
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        //隐藏
        SVProgressHUD.dismiss()
    }
    
    
    //可以从下面这个方法中获得授权码 (code)
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        
        let urlstring = request.URL?.absoluteString ?? ""
        
        //        print(urlstring)
        
        //截取字符串中含有code的字符串
        let successFlag = "code="
        
        if urlstring.containsString(successFlag) {
            
            //用! 表示一定能够找到对应字符串所在位置
            let range = urlstring.rangeOfString(successFlag)!
            
            let code = urlstring.substringFromIndex(range.endIndex)
            
            //            print(code)
            
            //有了授权码,接着请求获取token (用户访问令牌)
            LBMUserAccountViewModel.sharedAccountViewModel.loadAccessToken(code, finished: { (isSuccess) in
                
                if !isSuccess {
                    
                    //如果不成功
                    SVProgressHUD.showErrorWithStatus("世界上最遥远的距离就是没有网络")
                    return
                }
                
                //请求成功
                //授权成功,点击登录即切换视图,用通知 进入欢迎界面
                //
                //                UIApplication.sharedApplication().keyWindow?.rootViewController = LBMWelcomeViewController()
                
                //解决跳转之后原来视图还存在造成内存泄漏的问题
                self.dismissViewControllerAnimated(false, completion: {
                    
                    //发送通知
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: nil)
                    
                })
            })
            
            //不让它跳到黑马论坛页
            return false
            
        }
        
        return true
    }
    
    
    
    
    
    
}



























